package br.gendi.rcp.test;



import javax.imageio.ImageIO;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Search {

	static Browser browser;
	static String[] urls;
	static String[] titles;
	static int index;
	static int in=0;
	
	public static void main(String argv[]) {
		Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setText("SWT Browser - Documentation Viewer");
		shell.setLayout(new FillLayout());

		browser = new Browser(shell, SWT.MOZILLA);
		browser.setSize(640,480);
		browser.setUrl("www.yahoo.com");
		
		browser.addProgressListener(new ProgressListener() {

			@Override
			public void changed(ProgressEvent arg0) {

			}

			@Override
			public void completed(ProgressEvent arg0) { 	

				try {
					GC gc = new GC(browser);
					final Image image = new Image(arg0.display, browser.getSize().x, browser.getSize().y);
					gc.copyArea(image, 0, 0);
					gc.dispose();
					ImageLoader im = new ImageLoader();
					im.data = new ImageData[] { image.getImageData() };
					im.save("output__"+ in++ +".jpg", SWT.IMAGE_JPEG);
					System.out.println("impressao!");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		});

		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) display.sleep();
		}
		display.dispose();
	}
}


